﻿#region << 文件说明 >>
/*----------------------------------------------------------------
 * 命名空间：Vampirewal.Core.Tools
 * 唯一标识：8d8f3648-93de-47c8-a501-640c9fe04b29
 * 文件名：ZipHelper
 * 
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2025/6/4 22:13:36
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 文件说明 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Vampirewal.Core;

/// <summary>
/// 文件压缩帮助类
/// </summary>
public static class ZipHelper
{
    /// <summary>
    /// 压缩文件或目录到zip文件
    /// </summary>
    /// <param name="sourcePath">源文件或目录路径</param>
    /// <param name="zipPath">压缩后的zip文件路径</param>
    public static void Compress(string sourcePath, string zipPath)
    {
        try
        {
            // 检查源路径是否存在
            if (!File.Exists(sourcePath) && !Directory.Exists(sourcePath))
            {
                throw new FileNotFoundException($"源路径 {sourcePath} 不存在");
            }

            // 创建目标目录（如果不存在）
            string directory = Path.GetDirectoryName(zipPath);
            if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            // 压缩文件或目录
            if (File.Exists(sourcePath))
            {
                // 压缩单个文件
                using (FileStream fileStream = new FileStream(zipPath, FileMode.Create))
                using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
                {
                    zipArchive.CreateEntryFromFile(sourcePath, Path.GetFileName(sourcePath));
                }
            }
            else
            {
                if (File.Exists(zipPath))
                {
                    File.Delete(zipPath);
                }

                // 压缩整个目录
                ZipFile.CreateFromDirectory(sourcePath, zipPath);
            }

            Console.WriteLine($"文件/目录已成功压缩到 {zipPath}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"压缩失败: {ex.Message}");
            throw;
        }
    }

    public static void Compress(string[] filePaths, string zipPath)
    {
        if (filePaths == null || filePaths.Length == 0)
            return;

        string tempDirectory = Path.Combine(Path.GetTempPath(), "TempIdleHeroDirectory");
        try
        {

            Directory.CreateDirectory(tempDirectory);

            foreach (var filePath in filePaths)
            {
                if (!File.Exists(filePath))
                    throw new FileNotFoundException($"源路径 {filePath} 不存在");

                string fileName = Path.GetFileName(filePath);
                string destinationPath = Path.Combine(tempDirectory, fileName);
                File.Copy(filePath, destinationPath, true);
            }


            ZipFile.CreateFromDirectory(tempDirectory, zipPath);
        }
        catch (FileNotFoundException ex)
        {
            //// 删除临时目录
            //Directory.Delete(tempDirectory, true);
            throw ex;
        }
        finally
        {
            // 删除临时目录
            Directory.Delete(tempDirectory, true);
        }





    }

    /// <summary>
    /// 解压缩zip文件到指定目录
    /// </summary>
    /// <param name="zipPath">zip文件路径</param>
    /// <param name="extractPath">解压缩后的目录路径</param>
    /// <param name="overwriteFiles">是否覆盖</param>
    /// <param name="isDeleteZipFile">是否删除压缩文件</param>
    public static void Extract(string zipPath, string extractPath, bool overwriteFiles = true, bool isDeleteZipFile = false)
    {
        try
        {
            // 检查zip文件是否存在
            if (!File.Exists(zipPath))
            {
                throw new FileNotFoundException($"zip文件 {zipPath} 不存在");
            }

            // 创建目标目录（如果不存在）
            if (!Directory.Exists(extractPath))
            {
                Directory.CreateDirectory(extractPath);
            }

            // 解压缩zip文件
            ZipFile.ExtractToDirectory(zipPath, extractPath, overwriteFiles);
            Console.WriteLine($"zip文件已成功解压缩到 {extractPath}");

            if (isDeleteZipFile)
            {
                File.Delete(zipPath);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"解压缩失败: {ex.Message}");
            throw;
        }
    }
}
